#include"worker.h"
#include"my_tlv.h"
int tidArrInit(tidArr_t *ptidArr,int workerNum){
    //初始化子线程数组就是申请workerNum个pthread_t个类型的内存
    ptidArr->arr = (pthread_t *)calloc(workerNum,sizeof(pthread_t));
    //再把ptidArr的workerNum 初始化一下
    ptidArr->workerNum = workerNum;
    return 0;
}
void *threadFunc(void* arg){//目前子线程做命令匹配选择对应的服务
    threadPool_t *pthreadPool = (threadPool_t *)arg;

    pthread_mutex_lock(&pthreadPool->mutex);
    while (pthreadPool->taskQueue.queueSize == 0) {
        pthread_cond_wait(&pthreadPool->cond, &pthreadPool->mutex);
    }

    // 取出一个客户端连接
    int netfd = deQueue(&pthreadPool->taskQueue);
    pthread_mutex_unlock(&pthreadPool->mutex);

    // 接收客户端发送的 train_t 命令数据
    train_t train;
    memset(&train, 0, sizeof(train));
    recv(netfd, &train, sizeof(train_t), 0);

    // 命令结果结构体
    commandResult_t result = {0};
    result.result = NULL;

    // 使用 switch 判断命令类型
    switch (train.command) {
        case CD:
            command_cd(train.command_path, &result);
            break;
        case LS:
            command_ls(train.command_path, &result);
            break;
        case PWD:
            command_pwd(train.command_path, &result);
            break;
        case MKDIR:
            command_mkdir(train.command_path, &result);
            break;
        case RMDIR:
            command_rmdir(train.command_path, &result);
            break;
        case PUTS:
            command_puts(train.command_path, &result);
            break;
        case GETS:
            command_gets(train.command_path, &result);
            break;
        case REMOVE:
            command_remove(train.command_path, &result);
            break;
        default:
            printf("不支持的命令: %d\n", train.command);
            break;
    }

    // 打印或发送结果（这里只是打印，后续可改为 send 回客户端）
    if (result.result != NULL) {
        printf("命令执行结果：%s\n", result.result);
    }
    // 关闭连接
    close(netfd);

    return NULL;
}
